<?php
!defined('IN_FRAME') && die('404 Page');
class c_login extends Controller {
    
    private $objMdl = null;
    
    public function __construct() {
        $this->objMdl = Helper::loadModel('manager');
    }
    
    public function index() {
        $mdl = $this->objMdl;
        if ($mdl->logined()) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin');
        }
        $arrAssign = array(
            'strTitle' => '管理员登录',
            'strMsg' => '',
            'strName' => ''
        );
        if (is_array($_POST) && isset($_POST['sub_btn'])) {
            unset($_POST['sub_btn']);
            $bln = true;
            if ($bln && (!isset($_POST['username']) || empty($_POST['username']))) {
                $bln = false;
                $strMsg = '请输入帐号!';
            }
            $bln && $arrAssign['strName'] = $_POST['username'];
            if ($bln && (!isset($_POST['password']) || empty($_POST['password']))) {
                $bln = false;
                $strMsg = '请输入密码!';
            }
            if ($bln && (!isset($_POST['code']) || empty($_POST['code']))) {
                $bln = false;
                $strMsg = '请输入验证码!';
            }
            if ($bln && (!isset($_SESSION['check_img_code']) || time() > $_SESSION['check_img_code_expir'] || $_SESSION['check_img_code'] != $_POST['code'])) {
                $bln = false;
                $strMsg = '验证码错误!';
            }
            unset($_SESSION['check_img_code']);
            unset($_SESSION['check_img_code_expir']);
            if ($bln) {
                $arrTemp = $mdl->checkLogin($_POST['username'], $_POST['password']);
                if (!$arrTemp) {
                    $bln = false;
                    $strMsg = '错误的帐号或密码!';
                } else {
                    Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin');
                }
            }
            $arrAssign['strMsg'] = $strMsg;
        }
        $this->_display('admin/login.php', $arrAssign);
    }
    
    public function logout() {
        $this->objMdl->logout();
        Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login');
    }
}